
package com.snap.vseries.ui.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.snap.vseries.R;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class ExportProcessView {
    public static final int ERROR = 0x01;
    public static final int FINISH = 0x02;
    public static final int RUNNING = 0x03;
    private TextView contentTv;
    private TextButton closeTv;
    private Context context;

    private View layout;
    private AlertDialog dialog;

    public ExportProcessView(Context context, int type) {
        this.context = context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        switch (type) {
            case ERROR:
                layout = inflater.inflate(R.layout.export_process_error, null);
                closeTv = layout.findViewById(R.id.close_tv);
                contentTv = layout.findViewById(R.id.content_tv);
                break;
            case FINISH:
                layout = inflater.inflate(R.layout.export_process_finish, null);
                closeTv = layout.findViewById(R.id.close_tv);
                break;
            case RUNNING:
                layout = inflater.inflate(R.layout.export_process_run, null);
                break;
        }
        dialog = builder.create();
    }

    public ExportProcessView setContentTv(String content) {
        if (contentTv != null) {
            contentTv.setText(content);
        }
        return this;
    }

    public void show() {
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        Window window = dialog.getWindow();
        if (window != null) {
            //自定义布局应该在这里添加，要在dialog.show()的后面
            window.setContentView(layout);
            //允许弹出系统键盘
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            if (contentTv != null) {
                window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, AutoSizeUtils.dp2px(context, 88));
            } else {
                window.setLayout(AutoSizeUtils.dp2px(context, 260), AutoSizeUtils.dp2px(context, 88));
            }
            window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_white_8));
        }

        if (closeTv != null) {
            closeTv.setOnClickListener(v -> {
                dialog.dismiss();
            });
        }


    }

    public void dismiss() {
        dialog.dismiss();
    }

    public boolean isShowing() {
        return dialog.isShowing();
    }
}
